package com.app.qbake;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.app.qbake.module.login.LoginActivity;
import com.app.qbake.module.mine.MineFragment;
import com.app.qbake.module.mine.event.LogoutEvent;
import com.app.qbake.module.msg.MsgFragment;
import com.app.qbake.module.publish.activity.PublishGoodsActivity;
import com.app.qbake.module.square.SquareTabFragment;
import com.app.qbake.module.square.activity.SellerHomeActivity;
import com.app.qbake.module.video.VideoTabFragment;
import com.netease.nim.uikit.event.OpenTaHomeEvent;
import com.wind.base.BaseActivity;
import com.wind.base.repo.UserRepo;
import com.wind.base.utils.ActivityUtil;
import com.wind.base.utils.Navigator;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    public static final int TAB_HOME=0;
    public static final int TAB_VIDEO=1;
    public static final int TAB_MSG=2;
    public static final int TAB_MINE=3;
    @BindView(R.id.view_pager)
    ViewPager view_pager;
    @BindView(R.id.tab_home)
    View tab_home;
    @BindView(R.id.tab_video)
    View tab_video;
    @BindView(R.id.tab_msg)
    View tab_msg;
    @BindView(R.id.tab_mine)
    View tab_mine;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
       // SplashActivity.start(this);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        Fragment[] fragments=new Fragment[4];
        fragments[0]=new SquareTabFragment();
        fragments[1]=new VideoTabFragment();
        fragments[2]=new MsgFragment();
        fragments[3]=new MineFragment();
        MainPagerAdapter adapter=new MainPagerAdapter(getSupportFragmentManager(),fragments);
        view_pager.setAdapter(adapter);
        view_pager.setOffscreenPageLimit(4);
        tab_home.setActivated(true);

        AndPermission.with(this)
                .runtime()
                .permission(Permission.Group.STORAGE)
                .onGranted(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> permissions) {

                    }
                })
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> permissions) {

                    }
                })
                .start();
    }


    @OnClick({R.id.tab_home,R.id.tab_video,R.id.tab_msg,R.id.tab_mine,R.id.tab_publish})
    public void onViewClick(View view){

        switch (view.getId()){
            case R.id.tab_home:
                resetBottomBar();
                tab_home.setActivated(true);
                view_pager.setCurrentItem(TAB_HOME,false);
                break;
            case R.id.tab_video:
                resetBottomBar();
                tab_video.setActivated(true);
                view_pager.setCurrentItem(TAB_VIDEO,false);
                break;
            case R.id.tab_msg:
                if (UserRepo.getInstance().isLogin(this)) {
                    resetBottomBar();
                    tab_msg.setActivated(true);
                    view_pager.setCurrentItem(TAB_MSG, false);
                }else
                    LoginActivity.start(this);
                break;
            case R.id.tab_mine:
                if (UserRepo.getInstance().isLogin(this)) {
                    resetBottomBar();
                    tab_mine.setActivated(true);
                    view_pager.setCurrentItem(TAB_MINE, false);
                } else
                    LoginActivity.start(this);
                break;
            case R.id.tab_publish:
                if (UserRepo.getInstance().isLogin(this)){
                    PublishGoodsActivity.start(this);
                }else {
                    LoginActivity.start(this);
                }

                break;
        }
    }

    private void resetBottomBar() {
        tab_home.setActivated(false);
        tab_video.setActivated(false);
        tab_mine.setActivated(false);
        tab_msg.setActivated(false);
    }

    public static void start(Context activity) {
        Navigator.navigate(activity,MainActivity.class);
    }


    private class MainPagerAdapter extends FragmentPagerAdapter{

        Fragment[] fragments;
        public MainPagerAdapter(FragmentManager fm,Fragment[] fragments) {
            super(fm);
            this.fragments=fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments[position];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void on(LogoutEvent event){
        ActivityUtil.finish(this);
    }

    /**
     * 消息页点击他人头像
     * @param event
     */
    @Subscribe
    public void onOpenTaHomeEvent(OpenTaHomeEvent event){
        SellerHomeActivity.start(getActivity(),event.getUserId());
    }

}
